//
//  MyCommentVIew.swift
//  maosheng
//
//  Created by lxl on 2019/8/13.
//

import UIKit

class MyCommentVIew: PublicSwiftTableView {
    let noImgIdentifer = "MyCommentNoImgCell"
    let cellIdentifer = "MyCommentCell"

    var str_url = "api/services/app/Blog/GetMyBlogCommentPagingListAsync"

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        tableView.register(MyCommentNoImgCell.self, forCellReuseIdentifier: noImgIdentifer)
        tableView.register(MyCommentCell.self, forCellReuseIdentifier: cellIdentifer)

        getTheCommentList(isHeader: true)
        refreshList = {[weak self] (isHeader) in
            self?.getTheCommentList(isHeader: isHeader)
            
        }
        
        
    }
    
    func getTheCommentList(isHeader : Bool) -> Void {
       let url =  str_url + "?PageIndex=\(pageIndex)&PageSize=10"
        SDataCenter.shared.SGetJsonDataWithUrl(str_url: url, params: nil) { [weak self] (responseData, isSuccess) in
            if self?.tableView.mj_header != nil{
                self?.tableView.mj_header.endRefreshing()
                self?.tableView.mj_footer.endRefreshing()
            }
            
            if isSuccess && responseData is [String : Any]{
                if isHeader{
                    self?.dataSource.removeAllObjects()
                }
                if let items = (responseData as! [String : Any])["items"]{
                    if let arr = items as? Array<Any>{
                        if let attentionArr = [CommentModel].deserialize(from: arr){
                            self?.dataSource.addObjects(from: attentionArr as [Any])
                        }
                    }
                    
                }
                self?.checkNeedNoDataView()
                self?.setRefreshFooter()
            }else{
                if !isHeader{
                    self?.pageIndex = self!.pageIndex - 1
                }
                
            }
        }
    }
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let model = self.dataSource[indexPath.row] as? CommentModel
        let images = model?.blog?.images
        var cell : MyCommentNoImgCell?
       
        if images != nil && images!.count > 0{
            cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifer) as! MyCommentCell
        }else{
            cell = tableView.dequeueReusableCell(withIdentifier: noImgIdentifer) as? MyCommentNoImgCell
        }
        cell?.deleteBtn.addTarget(self, action: #selector(deleteComment(btn:)), for: .touchUpInside)
        
        cell?.deleteBtn.tag = 401 + indexPath.row
        cell!.setCommentInfo(model: model!)
        cell?.selectionStyle = .none
        return cell!
    }
    //MARK:-删除评论
    @objc func deleteComment(btn:UIButton) -> Void {
        
        PublicTools.showAlert(currentVC: nil, meg: "确定删除", cancelBtn: "取消", otherBtn: "确定") {[weak self] (action) in
            self?.deleteAction(btn: btn)
        }
        
        
    }
    
    func deleteAction(btn:UIButton) -> Void {
        let btnTag = btn.tag - 401
        let delUrl : String = "api/services/app/Comment/DeleteCommentAsync"
        let model = self.dataSource[btnTag] as? CommentModel
        var params = [String : Any]()
        params["id"] = model?.blogId

        SDataCenter.shared.SDeleteDataWithUrl(str_url: delUrl, params: params) { [weak self] (responseData, isSuccess) in
            if isSuccess{
                self?.dataSource.removeObject(at: btnTag)
                self?.tableView.reloadData()
                self?.checkNeedNoDataView()
            }
        }
    }
    func checkNeedNoDataView(){
        if self.dataSource.count == 0 {
            let str_msg : String = "还没有任何评论哦"
            self.showTheNoDataView(title: str_msg)
            self.noDataView?.noDataImg.image = UIImage(named: "comment_noData")
        }else{
            if self.noDataView != nil{
                self.noDataView?.isHidden = true
            }
        }
    }

}
